package com.evan.webadmin.biz.controller;


import com.evan.common.core.annotation.MyRequestBody;
import com.evan.common.core.constant.PersonnelTypeEnum;
import com.evan.common.core.object.MyPageData;
import com.evan.common.core.object.MyPageParam;
import com.evan.common.core.object.ResponseResult;
import com.evan.common.core.util.MyPageUtil;
import com.evan.webadmin.biz.dto.ExamDto;
import com.evan.webadmin.biz.dto.ExamDtoFilter;
import com.evan.webadmin.biz.dto.ExamFilter;
import com.evan.webadmin.biz.dto.ExamRecord;
import com.evan.webadmin.biz.helper.DictHelper;
import com.evan.webadmin.biz.model.Exam;
import com.evan.webadmin.biz.model.ExamPaper;
import com.evan.webadmin.biz.service.IExamPaperDetailService;
import com.evan.webadmin.biz.service.IExamPaperService;
import com.evan.webadmin.biz.service.IExamService;
import com.evan.webadmin.biz.vo.ExamFrontVo;
import com.evan.webadmin.biz.vo.ExamRankVo;
import com.evan.webadmin.biz.vo.ExamVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Pavel
 * @since 2024-06-10
 */
@RestController
@RequestMapping("/admin/biz/exam")
public class ExamController {

    @Autowired
    private IExamService examService;
    @Autowired
    private IExamPaperService examPaperService;
    @Autowired
    private IExamPaperDetailService examPaperDetailService;
    @Autowired
    DictHelper dictHelper;

    @PostMapping("/findByCondition")
    public ResponseResult<MyPageData<ExamVo>> findByCondition(@MyRequestBody ExamFilter examFilter,
                                                                 @MyRequestBody MyPageParam pageParam) {
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        List<ExamVo> exams = examService.listByCondition(examFilter.getExamType(), examFilter.getUserName());
        long totalCount = 0L;
        if (exams instanceof Page) {
            totalCount = ((Page<ExamVo>) exams).getTotal();
        }
        return ResponseResult.success(MyPageUtil.makeResponseData(exams, totalCount));
    }

    //题目编辑保存
    @PostMapping("/edit")
    public ResponseResult edit(@RequestBody ExamDto ExamDto) {
        examService.edit(ExamDto);
        return ResponseResult.success();
    }

    @PostMapping("/record")
    public ResponseResult<Exam> record(@RequestBody ExamRecord frontDto, HttpServletRequest request) {
        return examService.record(frontDto, request);
    }

    @PostMapping("/exist")
    public ResponseResult<Exam> exist(@RequestBody ExamDtoFilter filter, HttpServletRequest request) {
        return examService.exist(filter, request);
    }

    @PostMapping("/record/detail")
    public ResponseResult<ExamFrontVo> recordDetail(@RequestBody ExamDtoFilter filter, HttpServletRequest request) {
        return examService.recordDetail(filter, request);
    }

    @PostMapping("/rank")
    public ResponseResult<List<ExamRankVo>> rank(@RequestBody ExamDtoFilter filter) {
        return examService.rank(filter);
    }

    @PostMapping("/rankSelf")
    public ResponseResult<ExamRankVo> rankSelf(@RequestBody ExamDtoFilter filter, HttpServletRequest request) {
        return examService.rankSelf(filter, request);
    }

    @PostMapping("/rankPage")
    public ResponseResult<MyPageData<ExamRankVo>> rankPage(@MyRequestBody ExamDtoFilter examFilter,
                                                     @MyRequestBody MyPageParam pageParam) {

        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        if(examFilter.getSelectPaperId() == null){
            ExamPaper paper = examPaperService.findPublishedPaper(PersonnelTypeEnum.CUSTOM.getCode(), examFilter.getModuleType());
            if(paper != null){
                examFilter.setSelectPaperId(paper.getPaperId());
            }
        }
        List<ExamRankVo> examRankVos = examService.rankList(examFilter);
        long totalCount = 0L;
        if (examRankVos instanceof Page) {
            totalCount = ((Page<ExamRankVo>) examRankVos).getTotal();
        }
        return ResponseResult.success(MyPageUtil.makeResponseData(examRankVos, totalCount));
    }

}

